package com.haoyang.newairedu.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.haoyang.newairedu.WXResultBroadcast;
import com.jams.goodox.BuildConfig;
import com.jams.goodox.utils.DelayUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	/*
	1. 从服务器获取订单号  api/coin/recharge
      2. 从服务器获取微信参数。api/pay/placeOrder
      3. 利用参数进行微信支付

	NSDictionary *param = @{
		@"memberId":memberId,
		@"subject":@"好氧课程购买",
		@"outOrderId":orderNumber,
		@"outOrderNo":orderNumber,
		@"outOrderType”:@3,//1氧卡 购买 2 课程购买 3氧币购买
		@"payAmount":_priceNum.stringValue,
		@"device_info":@"ios",
		@"requestIp":@"",
	};
	教师：13146074589 ／ qwe123

家长： 18280094370 / qwe123
	*/
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//    	api = WXAPIFactory.createWXAPI(this, Constant.WEIXIN_APPID);
    	api = WXAPIFactory.createWXAPI(this, BuildConfig.WEIXIN_APPID);
        api.handleIntent(getIntent(), this);
    }


	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Intent intent = new Intent();
			intent.setAction(WXResultBroadcast.WXRESULT_ACTION);
			intent.putExtra("result",resp.errCode);
			sendBroadcast(intent);
			DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
				@Override
				public void doAction() {
					finish();
				}
			});

		}
	}
}